<?php
class Tasks extends CI_Model{
    
    /**
     * function construct
     * @author tien.nguyen
     */
    function __construct(){
        parent::__construct();
    }
    
    /**
     * Get list in click task
     * @author tien.nguyen
     * @return array
     */
    function getListTask($userId, $page){
        $configPagination = $this->config->item('number_pagination');
        $limit = ($page - 1)*$configPagination['task'];
        
        $this->db->select('tasks.*, users.FullName');
        $this->db->from('tasks');
        $this->db->join('users', 'users.UserId = tasks.CreatedUserId');
        $this->db->where(array('AssignUserId' => $userId));
        $this->db->order_by('tasks.CreatedDate DESC');
        $this->db->limit($configPagination['task'], $limit);
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function getTask($taskId){
        $query = $this->db->get_where("tasks", array("TaskId" => $taskId));
        return $query->row_array();
    }
    
    /**
     * Get count task
     * @author tien.nguyen
     */
    public function getCountTask($userId){
        $this->db->select('tasks.*, users.FullName');
        $this->db->from('tasks');
        $this->db->join('users', 'users.UserId = tasks.CreatedUserId');
        $this->db->where(array('AssignUserId' => $userId));
        return $this->db->count_all_results();
    }
    
    /**
     * Change status of task 
     * @author tien.nguyen
     */
    public function changeStatus($taskId, $status){        
        $data['TaskStatus'] = $status;
        
        return $this->db->update('tasks', $data, array('TaskId' => $taskId));
    }
}